package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.home.CityEntity;

import java.util.List;

/**
 * 搜索城市列表适配器
 */
public class ResultCityListAdapter extends BaseAdapter {
    private Context mContext;
    private List<CityEntity> mCities;
    private OnCityClickListener onCityClickListener;

    public ResultCityListAdapter(Context mContext, List<CityEntity> mCities) {
        this.mCities = mCities;
        this.mContext = mContext;
    }

    public ResultCityListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void changeData(List<CityEntity> list) {
        if (mCities == null) {
            mCities = list;
        } else {
            mCities.clear();
            mCities.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mCities == null ? 0 : mCities.size();
    }

    @Override
    public CityEntity getItem(int position) {
        return mCities == null ? null : mCities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ResultViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.city_result_item, parent, false);
            holder = new ResultViewHolder();
            holder.name = (TextView) view.findViewById(R.id.tv_seleteCityName);
            view.setTag(holder);
        } else {
            holder = (ResultViewHolder) view.getTag();
        }
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCityClickListener != null) {
                    onCityClickListener.onCityClick(mCities.get(position));
                }
            }
        });
        holder.name.setText(mCities.get(position).getCityName());
        return view;
    }

    public static class ResultViewHolder {
        TextView name;
    }

    public void setOnCityClickListener(OnCityClickListener listener) {
        this.onCityClickListener = listener;
    }

    public interface OnCityClickListener {
        void onCityClick(CityEntity cityEntity);
    }
}
